package com.jing.qq.qq;

/**
 * Created by Administrator on 2016/5/24.
 */
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jing.qq.adapter.MyPagerAdapter;
import com.jing.qq.util.Global;
import com.jing.qq.util.MyViewPager;

import java.util.ArrayList;
import java.util.List;

public class TalkActivity extends AppCompatActivity {

    //滑动效果设置
    private MyViewPager viewPager;
    private List<View> allViews;
    private MyPagerAdapter myPagerAdapter;

    //聊天页面设置
    private Button talk_back;
    private TextView talk_message_count;
    private TextView talk_name;
    private ListView talk_message;
    private TextView talk_phone;
    private TextView talk_person_setting;
    private Button talk_send_btn;
    private EditText talk_message_edit;
    private Button[] btn = new Button[7];
    private int[] footerBtnImgs = new int[]{R.drawable.skin_aio_panel_ptt_nor,R.drawable.skin_aio_panel_ptv_nor,
            R.drawable.skin_aio_panel_image_nor,R.drawable.skin_aio_panel_camera_nor,R.drawable.skin_aio_panel_hongbao_nor,
            R.drawable.skin_aio_panel_emotion_nor,R.drawable.skin_aio_panel_plus_nor};
    private int[] footerBtnImgsSelected = new int[]{R.drawable.skin_aio_panel_ptt_press,R.drawable.skin_aio_panel_ptv_press,
            R.drawable.skin_aio_panel_image_press,R.drawable.skin_aio_panel_camera_press,R.drawable.skin_aio_panel_hongbao_press,
            R.drawable.skin_aio_panel_emotion_press,R.drawable.skin_aio_panel_plus_press};
    private Class[] targetActivities = new Class[]{Main01Activity.class,Main02Activity.class,Main03Activity.class,
            Main04Activity.class,Main05Activity.class,Main06Activity.class,Main07Activity.class};

    //backView设置
    private Button start;

    //personView设置
    private TextView pv_tv;
    private Button pv_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talk);

        viewPager = (MyViewPager) findViewById(R.id.viewPager);
        allViews = new ArrayList<>();
        View datashowView = LayoutInflater.from(this).inflate(R.layout.activity_datashow,null);
        View talkView = LayoutInflater.from(this).inflate(R.layout.page_talk,null);
        View personView = LayoutInflater.from(this).inflate(R.layout.page_person,null);
        allViews.add(datashowView);
        allViews.add(talkView);
        allViews.add(personView);
        initDatashow(datashowView);
        initTalk(talkView);
        initPerson(personView);
        viewPager.setOffscreenPageLimit(3);
        myPagerAdapter = new MyPagerAdapter(allViews);
        viewPager.setAdapter(myPagerAdapter);
    }

    //聊天界面内的功能
    public void initTalk(View talkView){
        //返回按钮功能
        talk_back = (Button) talkView.findViewById(R.id.talk_back);
        talk_back.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action =motionEvent.getAction();

                if(action==MotionEvent.ACTION_DOWN){
                    talk_back.setBackgroundResource(R.drawable.skin_header_btn_back_press);
                }
                if(action==MotionEvent.ACTION_UP){
                    talkBackFunc();
                    talk_back.setBackgroundResource(R.drawable.skin_header_btn_back_normal);
                }
                return true;
            }
        });

        //信息数量
        talk_message_count = (TextView) talkView.findViewById(R.id.talk_message_count);
        talk_message_count.setText("信息(+0)");

        //显示名称
        talk_name = (TextView) talkView.findViewById(R.id.talk_name);
        String name = Global.setName;
        talk_name.setText(name);

        //聊天信息内容
        talk_message = (ListView) talkView.findViewById(R.id.talk_message);

        //视频电话
        talk_phone = (TextView) talkView.findViewById(R.id.talk_phone);
        talk_phone.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionEvent.getAction();
                if (action==MotionEvent.ACTION_DOWN){
                    talk_phone.setBackgroundResource(R.drawable.skin_aio_head_twocall_press);
                }
                if (action==MotionEvent.ACTION_UP){
                    talkPhoneFunc();
                    talk_phone.setBackgroundResource(R.drawable.skin_aio_head_twocall);
                }
                return true;
            }
        });

        //用户信息页面
        talk_person_setting = (TextView) talkView.findViewById(R.id.talk_person_setting);
        talk_person_setting.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionEvent.getAction();
                if (action == MotionEvent.ACTION_DOWN){
                    talk_person_setting.setBackgroundResource(R.drawable.skin_header_icon_single_selected);
                }if (action == MotionEvent.ACTION_UP){
                    talkPersonFunc();
                    talk_person_setting.setBackgroundResource(R.drawable.skin_header_icon_single);
                }
                return true;
            }
        });

        //消息
        talk_message_edit = (EditText) talkView.findViewById(R.id.talk_message_edit);

        //发送按钮
        talk_send_btn = (Button) talkView.findViewById(R.id.talk_send_btn);
        talk_message_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!("").equals(talk_message_edit.getText().toString())){
                    talk_send_btn.setBackgroundResource(R.drawable.talk_btn_send_press);
                }else {
                    talk_send_btn.setBackgroundResource(R.drawable.talk_btn_send);
                }
            }
        });

        //footer底部按钮
        btn[0] = (Button)talkView.findViewById(R.id.talk_ptt);
        btn[1] = (Button)talkView.findViewById(R.id.talk_ptv);
        btn[2] = (Button)talkView.findViewById(R.id.talk_image);
        btn[3] = (Button)talkView.findViewById(R.id.talk_camera);
        btn[4] = (Button)talkView.findViewById(R.id.talk_hongbao);
        btn[5] = (Button)talkView.findViewById(R.id.talk_emotion);
        btn[6] = (Button)talkView.findViewById(R.id.talk_plus);

        for (int i=0;i<btn.length;i++){
            final int temp = i;
            btn[temp].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    int action = motionEvent.getAction();
                    if (action==MotionEvent.ACTION_DOWN){
                        btn[temp].setBackgroundResource(footerBtnImgsSelected[temp]);
                    }
                    if (action==MotionEvent.ACTION_UP){
                        footerBtnFunc(temp);
                        btn[temp].setBackgroundResource(footerBtnImgs[temp]);
                    }
                    return true;
                }
            });
        }
    }
    //返回按钮的方法
    private void talkBackFunc () {
        viewPager.setCurrentItem(0,true);
    }

    //视频电话的方法
    private void talkPhoneFunc(){
        Toast.makeText(TalkActivity.this,"QQ电话",Toast.LENGTH_LONG).show();
    }

    //用户信息页面
    private void talkPersonFunc(){
        viewPager.setCurrentItem(2,true);
    }

    //footer按钮功能
    private void footerBtnFunc(int temp){
        Intent intent = new Intent(TalkActivity.this,targetActivities[temp]);
        startActivity(intent);
        finish();
    }


    //原假页面的方法
    public void initDatashow(View datashowView){
        start = (Button) datashowView.findViewById(R.id.start);
        Global.setName = start.getText().toString();
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1,true);
            }
        });
    }


    //原用户信息设置页面的方法
    public void initPerson(View personView){
        pv_tv =(TextView) personView.findViewById(R.id.pv_tv);
        pv_tv.setText(Global.setName);
        pv_btn = (Button) personView.findViewById(R.id.pv_btn);
        pv_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1,true);
            }
        });
    }
}

